<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="close"
    :title="$t('memberLists.sendPoint.发放积分')"
    width="400px"
  >
    <el-form :model="formData" label-width="120px" ref="formRef" :rules="rules">
      <el-form-item :label="$t('memberLists.sendPoint.变更类型')" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio :label="$t('memberLists.sendPoint.增加')" value="1" />
          <el-radio :label="$t('memberLists.sendPoint.减少')" value="2" />
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('memberLists.sendPoint.积分数量')" prop="point">
        <el-input-number v-model="formData.point" :min="0" :step="1" />
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script setup>
import { ref, watch, computed } from "vue";
import Dialog from "@/components/dialog.vue";
import Api from "@/config/api/api";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const show = defineModel();
const emit = defineEmits(["success"]);
const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});
watch(show, (val) => {
  if (val) {
    // props.data && (formData.value = props.data);
  }
});
const formData = ref({
  point: 0,
  type: "1",
});
const rules = computed(() => ({
  point: [{ required: true, message: t('memberLists.sendPoint.请输入积分数量'), trigger: "blur" }],
  type: [{ required: true, message: t('memberLists.sendPoint.请选择变更类型'), trigger: "blur" }],
}));
const formRef = ref();
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      Api.sendPoint_vip({
        id: props.data.id,
        point:
          formData.value.type == 1
            ? formData.value.point
            : -formData.value.point,
      }).then((res) => {
        if (res.status == 200) {
          ElMessage.success(t('memberLists.sendPoint.发放积分成功'));
          close();
          emit("success");
        }
      });
    }
  });
};
const close = () => {
  formData.value.point = 0;
  formData.value.type = "1";
  show.value = false;
};
</script>
